﻿/**********************************************************************************
 * 程序说明：     页面广告类(数据访问层)
 * 创建日期：     2009.6.13
 * 修改日期：     2012.02.15
 * 程序制作：     agui
 * 联系方式：     mailto:354990393@qq.com  
 * ********************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using PlugNT.Safe;
using PlugNT.Database;
using PlugNT.Database.Common.Simple;
using PlugNT.Custom;
using PlugNT.Cms.Model;

namespace PlugNT.Cms.DAL
{
    /// <summary>
    /// 页面广告类
    /// </summary>
    public class Attachment
    {

        private static string currTableName = WebConfig.TablePrefix + "attachment";



        private int GetDownCount(int id)
        {
            string sql = "Select top 1 [down_count] From " + currTableName + " where [id]=" + id.ToString();
            object obj = DbHelper.GetSingle(sql);
            return (obj == null) ? 0 : int.Parse(obj.ToString());
        }


        //public bool Exists(int id)
        //{
        //    string sql = "Select top 1 id From " + currTableName + " where [id]=" + id.ToString();
        //    return DbHelper.Exists(sql);
        //}

        /// <summary>
        /// 得到模型
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public AttachmentInfo GetAttachmentInfo(int id)
        {
            AttachmentInfo model = null;
            IDataReader dr = GetReader(1,"*", " and id=" + id);
            if (dr != null)
            {
                if (dr.Read())
                {
                    model = new AttachmentInfo();
                    model.id = int.Parse(dr["id"].ToString());
                    model.sys_model_id = int.Parse(dr["sys_model_id"].ToString());
                    model.title = dr["title"].ToString();
                    model.file_path = dr["file_path"].ToString();
                    model.file_type = dr["file_type"].ToString();
                    model.file_size = int.Parse(dr["file_size"].ToString());
                    model.down_count = int.Parse(dr["down_count"].ToString());
                    model.addtime = DateTime.Parse(dr["addtime"].ToString());
                }
                dr.Close();
            }
            return model;
        }





        #region 添加，修改，删除操作

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddAttachment(AttachmentInfo model)
        {


            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + currTableName);
            strSql.Append(" (sys_model_id,[title],[file_path],[file_type],[file_size],[down_count],[addtime]) ");
            strSql.Append(" values (");
            strSql.Append(" " + model.sys_model_id.ToString() + ",");
            strSql.Append("'" + StringHelper.SqlFilter(model.title) + "',");
            strSql.Append("'" + StringHelper.SqlFilter(model.file_path) + "',");
            strSql.Append("'" + StringHelper.SqlFilter(model.file_type) + "',");
            strSql.Append(" " + model.file_size.ToString() + ",");
            strSql.Append(" " + model.down_count.ToString() + ",");
            strSql.Append("'" + model.addtime.ToString() + "'");
            strSql.Append(")");


            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Update(AttachmentInfo model)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + currTableName);
            strSql.Append(" set [title]='" + StringHelper.SqlFilter(model.title) + "',");
            strSql.Append("[file_path]='" + StringHelper.SqlFilter(model.file_path) + "',");
            strSql.Append("[file_type]='" + StringHelper.SqlFilter(model.file_type) + "',");
            strSql.Append("[file_size]=" + model.file_size.ToString() + ",");
            strSql.Append("[down_count]=" + model.down_count.ToString() + ",");
            strSql.Append("[start_time]='" + model.addtime.ToString() + "'");
            strSql.Append(" where id=" + model.id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }

        public bool OperationDownCount(int id, string operation)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + currTableName);
            strSql.Append("down_count=down_count" + (operation == "+" ? "+" : "-"));
            strSql.Append(" where id=" + id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete From " + currTableName);
            strSql.Append(" where [id]="+id.ToString());
            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }

        #endregion


        #region 获取列表

        /// <summary>
        /// 得到IDataReader
        /// </summary>
        /// <param name="topCount">-1表示全部</param>
        /// <param name="colNames"></param>
        /// <param name="andWhere"></param>
        /// <returns></returns>
        public IDataReader GetReader(int topCount, string colNames, string andWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select " + (topCount == -1 ? "" : "top " + topCount) + " " + colNames + " from " + currTableName + " where 1=1 " + andWhere);
            strSql.Append(WebConfig.IsAccess ? " order by [order] desc" : " order by [order],id desc");
            return DbHelper.ExecuteReader(strSql.ToString());
        }

        public DataTable GetList(int sysModelId, string andWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from " + currTableName + " where sys_model_id= " + sysModelId + andWhere);
            strSql.Append(WebConfig.IsAccess ? " order by [order] desc" : " order by [order],id desc");
            return DbHelper.GetDataTable(strSql.ToString());
        }

        public DataTable GetList(int sysModelId, int topCount, int currPageCount, string andWhere)
        {
            string sql = Sql.GetRePagingString(topCount, currPageCount, currTableName, "id", " and sys_model_id= " + sysModelId + andWhere);
            return DbHelper.GetDataTable(sql);
        }

        public DataTable GetList(int sysModelId, int topCount, int currPageCount, string andWhere, LeftJoinInfo joininfo)
        {
            string sql = Sql.GetRePagingString(topCount, currPageCount, currTableName, "id", "*", " and sys_model_id= " + sysModelId + andWhere, joininfo);
            return DbHelper.GetDataTable(sql);
        }


        public int GetCount(int sysModelId, string andWhere)
        {
            string sql = "select count(1) from [" + currTableName + "] where sys_model_id= " + sysModelId + andWhere;
            return (int)DbHelper.GetSingle(sql);
        }

        #endregion
    }
}
